/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: general_sender.h
 *
 * Purpose: a general implmentation for sender API define
 *
 * Developer:
 *   wen.gu , 2023-06-25
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __UDSSIPC_GENERAL_SENDER_H__
#define __UDSSIPC_GENERAL_SENDER_H__

#include <list>
#include <mutex>
#include <condition_variable>

#include "core/sender.h"
#include "core/thread.h"
#include "core/message_queue.h"

class GeneralSender: public Sender, public Thread{
public:
    GeneralSender(const std::string& sender_name, ComFd uds_fd, uint32_t max_queue_size = 0); /**max_queue_size set as 0, inidicat unlimite the queue size */
    virtual ~GeneralSender();

public:
    virtual ComErrC send(ComMessagePtr msg_ptr);
    virtual ComErrC send(ComMessageType type, uint8_t* payload_data, uint32_t payload_size);

    /** this API will alloc message with 'payload_size' and call 'fill_handler' method to fill payload to message */
    virtual ComErrC send(ComMessageType type, uint32_t payload_size, PayloadFillHandler fill_handler);

public:
        /** if some error occur(e.g the connection is borken), then the sendder will be 'suspend', so need resume the state before call 'send' when error is resolved */
    virtual ComErrC resume();

    virtual ComErrC clear(); // clear(drop) all data cached in sender wwhich to be sent.
    virtual ComErrC flush(uint32_t timeout_ms); //wait for all message in sender to be sent.

protected:
    virtual void run();
private:
    bool is_running_;
    MessageQueue* msg_list_;
};





#endif /** !__UDSSIPC_GENERAL_SENDER_H__ */